import time

from selenium.webdriver.common.by import By

from setting import create_chrom_driver, dbsql, save_mysql,close_mysql

browser = create_chrom_driver()
browser.get(
    'https://passport.lagou.com/login/login.html')

create_table = '''
                    create table if not exists lagou(name varchar(50),education varchar(50),
                                salary varchar(30),welfare varchar(100))
                                default charset = utf8;
                                '''
time.sleep(50)

def save_(data):
    save_data = '''insert into lagou_data values (%s,%s,%s,%s); '''
    save_mysql(save_data, data)


def data_info():
    browser.find_element(By.CLASS_NAME, 'lg-pagination-next').click()
    time.sleep(5)
    divs = browser.find_elements(By.CSS_SELECTOR, '.item__10RTO')
    for div in divs:
        position = div.find_element(By.CSS_SELECTOR, '.p-top__1F7CL a').text
        company = div.find_element(By.CSS_SELECTOR, '.company-name__2-SjF a').text
        salary = div.find_element(By.CLASS_NAME, 'p-bom__JlNur').text
        welfare = div.find_element(By.CLASS_NAME, 'il__3lk85').text
        list_data = [position, company, salary, welfare]
        save_(list_data)
        print("正在爬取！！")

for pag in range(30):
    time.sleep(1)
    data_info()

# dbsql(create_table)

browser.quit()
close_mysql()